<template>
	<view class="content">
		<view class="head">
			<u-avatar v-if="viewMsg.img !== ''" :src="viewMsg.img" mode="circle"></u-avatar>
			<u-avatar v-else :src="user.img" mode="circle"></u-avatar>
			<view class="name_grade">
				<view v-if="viewMsg.name !== ''" class="name">{{viewMsg.name}}</view>
				<view v-else class="name">{{user.name}}</view>
				<view class="grade">万种鲜，唯有美食天下鲜!</view>
			</view>
			<view class="set_up">
				<text>个人信息</text>
				<u-icon name="arrow-right"></u-icon>
			</view>
		</view>
		
		<view class="id_card">
			<view class="id_card_grade">
				等级:{{viewMsg.uGrade === null || viewMsg.uGrade === undefined ?0:viewMsg.uGrade}}
				<text class="id_card_grade_text" v-if="viewMsg.Shop === 0">(食客)</text>
				<text class="id_card_grade_text" v-else>(厨子)</text>
			</view>
			<view class="id_card_msg">
				<view class="">
					点评:{{viewMsg.msgTotal === null || viewMsg.msgTotal ===undefined ? 0 : viewMsg.msgTotal}}
				</view>
				<view class="">
					获赞:{{viewMsg.uPraise === null || viewMsg.uPraise === undefined ? 0 : viewMsg.uPraise}}
				</view>
				<view class="">
					被贬:{{viewMsg.uComplaint === null || viewMsg.uComplaint === undefined ? 0 : viewMsg.uComplaint}}
				</view>
			</view>
		</view>
		
		<view class="mine" v-if="viewMsg.Shop === 0">
			<view class="mine_title">
				应用
			</view>
			<view class="mine_operation">
				<view class="console">
					<navigator url="/pages/discuss/discuss">
						<u-icon name="edit-pen"></u-icon>
						发表点评
					</navigator>
				</view>
				<view class="console">
					<navigator url="/pages/myDiscuss/myDiscuss">
						<u-icon name="chat"></u-icon>
						我的点评
					</navigator>
				</view>
				<view class="console" @click="undone">
					<navigator>
						<u-icon name="warning"></u-icon>
						我的投诉
					</navigator>
				</view>
				<view class="console">
					<navigator url="/pages/becomeAMerchant/becomeAMerchant">
						<u-icon name="file-text"></u-icon>
						商家入驻
					</navigator>
				</view>
			</view>
		</view>
		
		<view class="shop" v-else>
			<view class="mine_title">
				应用
			</view>
			<view class="mine_operation">
				<view class="console">
					<navigator url="/pages/discuss/discuss">
						<u-icon name="edit-pen"></u-icon>
						发布美食
					</navigator>
				</view>
				<view class="console">
					<navigator url="/pages/myDiscuss/myDiscuss">
						<u-icon name="order"></u-icon>
						我的发布
					</navigator>
				</view>
				<view class="console">
					<navigator url="/pages/shopMsg/shopMsg">
						<u-icon name="file-text-fill"></u-icon>
						店铺信息
					</navigator>
				</view>
				<view class="console">
					<navigator url="/pages/suggestion/suggestion">
						<u-icon name="edit-pen-fill"></u-icon>
						修改建议
					</navigator>
				</view>
			</view>
		</view>
			
		<view class="message">
			<view class="">
				声明
				<u-icon name="arrow-right"></u-icon>
			</view>
			<view class="">
				帮助
				<u-icon name="arrow-right"></u-icon>
			</view>
		</view>
		
		<view class="login_out">
			<u-button shape="circle" v-if="vuex_token === null || vuex_token === ''" @click="login">登陆</u-button>
			<u-button shape="circle" v-else @click="noLogin">注销</u-button>
		</view>
		
	</view>
</template>

<script>
	import bus from '../../utils/index.js'
	export default {
		data() 
		{
			return {
				title: 'Hello',
				user:{
					img:'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
					name:'侠名'
				},
				viewMsg:{
					img:'',
					name:'',
					msgTotal:0,
					Shop:0,
					uPraise:0,
					uComplaint:0,
					uGrade:0
				}

			}
		},
		onLoad() 
		{
			this.renovate()
		},
		onShow()
		{
			this.renovate()
		},
		methods: {
			login()
			{
				// console.log('被点击');
				if(this.vuex_token)
				{
					this.$u.vuex('vuex_token',null)
				}
				else
				{
					var that = this
					uni.showModal({
						mask: true,
						title: '温馨提示',
						content: '授权微信登录后才能正常使用小程序功能',
						success(res) 
						{
							if (res.confirm) 
							{
								uni.getUserProfile({
									desc: '获取你的昵称、头像',
									success: userRes => {
										if (userRes.errMsg == 'getUserProfile:ok' && userRes.userInfo != undefined) 
										{
											var userInfo = {
												avatarUrl: userRes.userInfo.avatarUrl,
												nickName: userRes.userInfo.nickName
											}
											//对页面中的变量进行赋值
											that.viewMsg.name = userRes.userInfo.nickName
											that.viewMsg.img = userRes.userInfo.avatarUrl
											// 调用接口请求openid
											that.getUserOpenId(userInfo)
										} 
										else 
										{
											uni.showToast({
												icon:"none",
												title:"获取失败，请重试"
											})
										}
									},
									fail: error => {}
								});
							} else if (res.cancel) {}
						}
					})
				}
			},
			getUserOpenId(userInfo)
			{
				var that = this
				uni.login({
					provider: 'weixin',
					success(loginAuth) 
					{
						var data = {'code':loginAuth.code}
						var path = '/login/getOpenId'
						that.$login(path,data).then((response)=>{
							userInfo['openid'] = response
							//缓存用户详细在vuex中
							that.$u.vuex('vuex_token',userInfo)
							// console.log('前台的数据',userInfo)
							let params = {
								uId: userInfo.openid,
								uAvatar: userInfo.avatarUrl,
								uName: userInfo.nickName
							}
							let res = that.$u.api.intUpdUserView(params)
							that.getByIdUserMsg(userInfo.openid)
						})
					}
				})
				
				
				// bus.$emit('daily','msg')
			},
			async getByIdUserMsg(val)
			{
				let res = await this.$u.api.getByIdUser({id:val})
				this.$u.vuex('vuex_person',res)
				this.renovate()
			},
			async renovate()
			{
				if(this.vuex_token)
				{
					let res = await this.$u.api.getByIdUser({id:this.vuex_token.openid})
					this.$u.vuex('vuex_person',res)
					// console.log('前台的数据:',this.vuex_person);
					if(this.vuex_person)
					{
						this.viewMsg.uGrade = Math.trunc((this.vuex_person.uPraise - this.vuex_person.uComplaint) / 100 + 1)
						this.viewMsg.uPraise = this.vuex_person.uPraise
						this.viewMsg.uComplaint = this.vuex_person.uComplaint
						this.viewMsg.Shop = this.vuex_person.uShop
						this.viewMsg.img = this.vuex_person.uAvatar
						this.viewMsg.name = this.vuex_person.uName
					}
					let params = {
						pageNum:1,
						pageSize:5,
						id:this.vuex_token.openid
					}
					res = await this.$u.api.getByIdDiscuss(params)
					this.viewMsg.msgTotal = res.total
					//刷新推荐数据
					console.log("index,msg执行");
					bus.$emit('index','msg')
				}
			},
			undone()
			{ 
				this.$u.toast('未开发')
			},
			noLogin()
			{
				this.$u.vuex('vuex_token',null)
				this.$u.vuex('vuex_person',null)
				this.viewMsg.msgTotal = 0
				this.viewMsg.Shop = 0
				this.viewMsg.uPraise = 0
				this.viewMsg.uComplaint = 0
				this.viewMsg.uGrade = 0
				this.viewMsg.img = ''
				this.viewMsg.name = ''
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import "@/pages/center/index.scss";
	.shop
	{
		margin-top: 40rpx;
	}
</style>
